﻿using CommunityToolkit.Mvvm.ComponentModel;
using FanaticRock.Core.Extensions;
using FanaticRock.Core.Helpers;
using FanaticRock.Core.Interfaces;
using FanaticRock.Core.Models;
using FanaticRock.DownloadManage.Helpers;

namespace FanaticRock.ViewModels.Download
{
    //file enum SongLocalSourceEnum
    //{
    //    FromWeb, FromLocal
    //}

    //file  static class SongLocalSourceHelper 
    //{
    //}

    ///<summary>用以显示进度的ViewModel</summary>
    public partial class DownloadBvidSongViewModel : BvidSongViewModel, ILogger
    {
        #region fields

        private string _baseUri;
        private string _taskName;

        #endregion

        #region props

        public string TaskName => _taskName;
        public string BaseUri => _baseUri;

        [ObservableProperty]
        private double _progress = 0;

        [ObservableProperty]
        private bool _isCompleted = false;

        [ObservableProperty]
        protected string _localPath;

        #endregion

        #region ctors

        public DownloadBvidSongViewModel() : base()
        {

        }

        public DownloadBvidSongViewModel(BvidSongViewModel source) : base(source)
        {

        }

        public DownloadBvidSongViewModel(DownloadItem downloadItem) : base()
        {
            _id = downloadItem.Id;
            _title = downloadItem.Title;
            _originTitle = downloadItem.Title;
            _albumTitle = downloadItem.Title;
            _duration = TimeSpan.Zero;

            _baseUri = downloadItem.SourceUri;
            _uri = UriHelper.GetUri(downloadItem.LocalPath);
            _localPath = downloadItem.LocalPath;
            SetCoverSource(downloadItem);
            //_members = viewModel.Members;

            IndexInCollection = 0;

            _isCompleted = downloadItem.IsCompleted;
        }



        #endregion

        #region methods

        private void SetCoverSource(DownloadItem downloadItem)
        {
            var isLocal = DownloadItemHelper.GetLocalSourceInLocalStorage(downloadItem); // 判断是否已经存放在本地。

            if (isLocal)
            {
                _coverSource = new InAudioFileResourceSource(UriHelper.GetLocalUri(downloadItem.LocalPath));
            }
            else
            {
                _coverSource = new UriResourceSource(UriHelper.GetUri(downloadItem.SourceUri));
            }

        }


        public DownloadBvidSongViewModel Init(string taskName, string uri, string localPath)
        {
            _baseUri = uri;
            _taskName = taskName;
            LocalPath = localPath;
            Uri = UriHelper.GetUri(localPath);
            return this;
        }


        public override async void Play()
        {
            if (_isCompleted == false)
            {
                this.Info($"DownloadBvidSongViewModel.Play: {Title} 曲子未下载完成，无法播放");
                return;
            }



            base.Play();
        }

        #endregion

    }
}
